module Fighter {
	/**联机模式--by王石 */
	export class OlUI extends eui.Component {
		/**联机模式单例 */
		public static OlUI_solo: OlUI;
		/**匹配按钮 */
		public marryBtn: eui.Group;
		/**匹配窗口 */
		public marryStata: eui.Group;

		/**匹配结果 */
		public marryMsg: eui.BitmapLabel;
		/**倒计时 */
		public count_down: egret.Timer;
		/**倒计时数字 */
		public count_down_number: number;
		/**取消匹配按钮 */
		public cancelBtn: eui.Image;
		/**是否关闭 */
		public is_close: boolean;
		/**是否匹配上人 */
		public is_match: boolean;
		/**虚假头像 */
		public fake_EnemyHead: eui.Image;
		/**显示的容器 */
		private showPanel: any;
		/**成功匹配 */
		private successMarry: eui.Group;
		/**邀请按钮 */
		private playWithFriendBtn: eui.Group;
		/**匹配中 图片 */
		private loading: eui.Image;
		/**联网匹配按钮框 */
		private onLine: eui.Group;
		/**返回按钮 */
		public backBtn: eui.Image;
		/**返回主页布尔值 */
		public backMainPage: boolean;
		public constructor() {
			super();
			this.skinName = "resource/assets/PlaneE/Ol_UI.exml";
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.add_to_stage, this);
			this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);
			OlUI.OlUI_solo = this;

		}
		private add_to_stage() {
			this.is_close = false;
			this.is_match = false;
			this.backMainPage = false;
			this.fake_EnemyHead.visible = false;
			this.backBtn.visible = true;

			this.onLine.visible = true;
			this.marryStata.visible = false;
			this.cancelBtn.visible = false;
			this.successMarry.visible = false;
			this.marryBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, webNetworkMananger.getInstance().user_ready, this);
			this.marryBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.marry, this);

			this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.cancel_marry, this);
			this.playWithFriendBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.Invitation, this);
			this.playWithFriendBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, webNetworkMananger.getInstance().inviteFriend, this);
			this.backBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.backMain, this);
			if (StartUI.invited) {
				webNetworkMananger.getInstance().inviteFriend();
				this.onLine.visible = false;
				this.backBtn.visible = false;
			}
		}
		/**离开舞台 */
		private removeFromStage() {
			if (Main.wxHead && Main.wxHead.parent) {
				Main.wxHead.parent.removeChild(Main.wxHead);
			}
			if (Main.EnemyHead && Main.EnemyHead.parent) {
				Main.EnemyHead.parent.removeChild(Main.EnemyHead);
			}
		}
		/**显示己方头像 进行匹配 */
		private showHead() {
			// //加载微信头像
			if (Main.wxHead) {
				Main.wxHead.x = 79;
				Main.wxHead.y = 361;
				this.addChild(Main.wxHead);
			}
		}
		/**点击匹配 */
		private marry() {
			this.backBtn.visible = false;
			this.onLine.visible = false;
		}

		/**邀请好友结果    服务器*/
		public async Invitation() {
			await platform.activeShare(messageManage.userid);
			this.onLine.visible = false;
			this.marryStata.visible = true;
			this.loading.visible = false;
			this.showHead();
			this.backBtn.visible = false;
		}
		/**邀请成功 */
		public friendLogin() {
			//this.onLine.visible = false;
			//this.showHead();
			this.successMarry.visible = true;
			if (Main.EnemyHead) {
				Main.EnemyHead.x = 418;
				Main.EnemyHead.y = 361;
				this.addChild(Main.EnemyHead);
			}
		}
		/**准备结果 */
		public ready_result(tmpmessage: awesomepackage.s2cReady) {
			this.marryStata.visible = true;
			this.showHead();
			if (tmpmessage.errorCode == 0) {
				this.count_down_number = 0;
				this.count_down = new egret.Timer(1000, 0);
				this.count_down.addEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
				this.count_down.start();

			} else {

				//this.cancelBtn.visible = true;
			}
		}
		/**匹配倒计时显示 
		 * 超过60秒 自动匹配人机by严皓
		*/
		public count_down_show() {
			this.cancelBtn.visible = false;
			
			this.count_down_number++;
			if (this.count_down_number >= 60) {
				this.marryMsg.text = (65 - this.count_down_number).toString();
				this.cancelBtn.visible = false;
				this.successMarry.visible = true;
				this.fake_EnemyHead.visible = true;
				if (this.count_down_number >= 65) {					
					this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
					this.count_down.stop();
					GameUtil.PlayerMessage.instance.onePerson = true;
					this.is_match = false;
					StartUI.StartUI_sole.is_close = true;
					this.is_close = true;
				}			
			}
			else {
				this.marryMsg.text = (90 - this.count_down_number).toString();
			}
		}
		/**匹配结果 */
		public marry_result(tmpmessage: awesomepackage.s2cGameStart) {
			if (tmpmessage.errorCode == 0) {
				this.is_match = true;
				this.count_down_number = 0;
				this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
				this.count_down.stop();
				//成功匹配			
				//this.successMarry.visible = true;
				this.count_down = new egret.Timer(1000, 0);
				this.count_down.addEventListener(egret.TimerEvent.TIMER, this.Start_count_down, this);
				this.count_down.start();
			}
		}
		/**开始游戏倒计时 */
		public Start_count_down() {
			this.cancelBtn.visible = false;
			//this.onLine.visible = false;
			this.count_down_number++;
			this.marryMsg.text = (5 - this.count_down_number).toString();
			this.successMarry.visible = true;
			if (this.count_down_number >= 5) {
				
				this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.Start_count_down, this);
				this.count_down.stop();
				StartUI.StartUI_sole.is_close = true;
				this.is_match = false;
				this.is_close = true;
			}
			//加载微信头像
			if (Main.wxHead) {
				Main.wxHead.x = 79;
				Main.wxHead.y = 361;
				this.addChild(Main.wxHead);
			}
			if (Main.EnemyHead) {
				Main.EnemyHead.x = 418;
				Main.EnemyHead.y = 361;
				this.addChild(Main.EnemyHead);
			}

		}
		/**返回开始界面 */
		public backMain() {
			this.backMainPage = true;
		}
		/**取消匹配 */
		public cancel_marry() {
			this.cancelBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.cancel_marry, this);
			this.marryStata.visible = false;
			this.onLine.visible = true;
		}
	}
}
